﻿using System;
using SomethingSpacial.CustomControls;
using SomethingSpacial.Model;
using System.Collections.ObjectModel;
using SomethingSpacialClassLib.Web;
using System.ServiceModel.DomainServices.Client;

namespace SomethingSpacial.ViewModel
{
    public class DefaultViewModel : ViewModelBase
    {
        private DefaultModel MyModel = new DefaultModel();

        private SomethingSpacialContext domainContext;
        private SomethingSpacialContext DomainContext
        {
            get
            {
                if (domainContext == null)
                    domainContext = new SomethingSpacialContext();
                return domainContext;
            }
        }
        LoadOperation LoadUserGroups;

        public DefaultViewModel()
        {
            UserGroups = new ObservableCollection<UserGroupModel>();
        }

        public static ObservableCollection<UserGroupModel> UserGroups
        {
            get;
            private set;
        }

        #region "ViewModel Methods"

        public void loadUserGroups()
        {
            LoadUserGroups = DomainContext.Load(DomainContext.GetUserGroupsFirstQuery());
            LoadUserGroups.Completed += new EventHandler(LoadUserGroups_Completed);
        }

         void LoadUserGroups_Completed(object sender, EventArgs e)
        {

            //Not sure why this is required.  Wouldn't it have to be "Complete" in order for this UserGroup to be triggered? (ds 09/26/09)
            if (LoadUserGroups.IsComplete == true)
            {
                foreach (Entity entity in LoadUserGroups.Entities)
                {
                    UserGroup UserGroup = entity as UserGroup;

                   DisplayTitle =  UserGroup.UG_Name;
                   Description = UserGroup.UG_Description;
                   TwitterURL = UserGroup.UG_TweetSource;

                }
            }

        }

        #endregion




        public string HeroTextTitle
        {
            get { return MyModel.HeroTextTitle; }
            set
            {
                if (MyModel.HeroTextTitle != value)
                {
                    MyModel.HeroTextTitle = value;
                    InvokePropertyChanged("HeroTextTitle");
                }
            }
        }

        public  string DisplayTitle
        {
            get { return MyModel.DisplayTitle; }
            set
            {
                if (MyModel.DisplayTitle != value)
                {
                    MyModel.DisplayTitle = value;
                    InvokePropertyChanged("DisplayTitle");
                }
            }
        }

        public string Description
        {
            get { return MyModel.Description; }
            set
            {
                if (MyModel.Description != value)
                {
                    MyModel.Description = value;
                    InvokePropertyChanged("Description");
                }
            }
        }

        public string NewsTitle
        {
            get { return MyModel.NewsTitle; }
            set
            {
                if (MyModel.NewsTitle != value)
                {
                    MyModel.NewsTitle = value;
                    InvokePropertyChanged("NewsTitle");
                }
            }
        }

        public string BlogRollTitle
        {
            get { return MyModel.BlogRollTitle; }
            set
            {
                if (MyModel.BlogRollTitle != value)
                {
                    MyModel.BlogRollTitle = value;
                    InvokePropertyChanged("BlogRollTitle");
                }
            }
        }

        public static string NewsFeed
        {
            get { return DefaultModel.NewsFeed; }
        }

        public static string LoginText
        {
            get { return DefaultModel.LoginText; }
        }

        public static string LogoutText
        {
            get { return DefaultModel.LogoutText; }
        }

        public static string ForegroundColor
        {
            get { return DefaultModel.ForegroundColor; }
        }

        public string TwitterURL
        {
            get { return MyModel.TwitterURL; }
            set
            {
                if (MyModel.TwitterURL != value)
                {
                    MyModel.TwitterURL = value;
                    InvokePropertyChanged("TwitterURL");
                }
            }



        }


    }
}
